package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.media.dto.SensitiveDto;
import com.heima.media.entity.WmSensitive;
import com.heima.media.mapper.WmSensitiveMapper;
import com.heima.media.service.IWmSensitiveService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 敏感词信息表 服务实现类
 * </p>
 *
 * @author lqw
 * @since 2022-06-24
 */
@Service
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements IWmSensitiveService {
    /**
     * 敏感词分页查询
     * @param dto
     * @return
     */
    @Override
    public ResponseResult pageList(SensitiveDto dto) {
        LambdaQueryWrapper<WmSensitive> query = new LambdaQueryWrapper<>();
        IPage<WmSensitive> page = new Page<>(dto.getPage(),dto.getSize());
        if (!StringUtils.isEmpty(dto.getName())){
            query.like(WmSensitive::getSensitives,dto.getName());
        }
        IPage<WmSensitive> iPage = this.page(page, query);
        ResponseResult result = new PageResponseResult(dto.getPage().longValue(), dto.getSize(), iPage.getTotal(), iPage.getRecords());
        return result;
    }

    /**
     * 修改敏感词
     * @param sensitive
     * @return
     */
    @Override
    public ResponseResult updateSensitive(WmSensitive sensitive) {
        //查询修改的敏感词之前数据库是否存在
        LambdaQueryWrapper<WmSensitive> query = new LambdaQueryWrapper<>();
        query.eq(WmSensitive::getSensitives,sensitive.getSensitives());
        WmSensitive one = this.getOne(query);
        if (!StringUtils.isEmpty(one)){
          return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        this.updateById(sensitive);
        return ResponseResult.okResult();
    }
}
